///*
// * To change this template, choose Tools | Templates
// * and open the template in the editor.
// */
//
package researchgrants.parts.pi.departments;


import java.awt.BorderLayout;
import java.util.SortedMap;
import researchgrants.parts.LoggedData.panels.*;
import java.awt.Component;
import java.awt.FlowLayout;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import researchgrants.parts.LoggedData.panels.lists.LoggedDataPanelsList;

/**
 *
 * @author DOStudent1
 */
public class LoggedDataPanelsListDepartment extends LoggedDataPanels  {

    private void checkInitializedData() {
        if (!isParentInitialized()) {
            throw new RuntimeException("Didn't set parent");
        }
    }
    
    JPanel pnlViewContent;
    
    // re/create the view content
    public void createView() {
        pnlViewContent.removeAll();
        String data=super.getData().value();
        if (!data.equals("")) {
            Department department=Department.getByDepartmentName(data);
            School school=department.getSchool();

            JPanel pnlContentSchool=new JPanel(new FlowLayout(FlowLayout.LEFT, 2,0));
            pnlContentSchool.add(new JLabel("School: "));
            pnlContentSchool.add(new JLabel(school.getSchoolName()));
            pnlViewContent.add(pnlContentSchool, BorderLayout.NORTH);

            JPanel pnlContentDepartment=new JPanel(new FlowLayout(FlowLayout.LEFT, 2,0));
            pnlContentDepartment.add(new JLabel("Department: "));
            pnlContentDepartment.add(new JLabel(department.getDepartmentName()));
            pnlViewContent.add(pnlContentDepartment, BorderLayout.SOUTH);
        }
        pnlViewContent.revalidate();
        getParentWindow().pack();
    }

    @Override
    public Component getViewComponent() {
        checkInitializedData();
        pnlViewContent=new JPanel(new BorderLayout(0, 0));
        createView();
        return(pnlViewContent);
    }

    
    JPanel pnlSchools;
    JPanel pnlDepartments;
    JPanel pnlEditContent;

    LoggedDataPanelsList schoolsPanels;
    LoggedDataPanelsList departmentsPanels;
    
    @Override
    public Component getEditComponent() {
        pnlEditContent=new JPanel(new BorderLayout(0, 0));

        pnlSchools=new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        pnlDepartments=new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));

        pnlEditContent.add(pnlSchools, BorderLayout.NORTH);
        pnlEditContent.add(pnlDepartments, BorderLayout.SOUTH);
        
        createSchoolsList();

        return(pnlEditContent);
    }
    
    // re/create the agencies list
    private void createSchoolsList() {
        pnlSchools.removeAll();
        
        schoolsPanels=new LoggedDataPanelsListSchools();

        schoolsPanels.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                schoolSelectionChanged();
            }

        });
        
        pnlSchools.add(new JLabel("School:"));
        pnlSchools.add(schoolsPanels.getEditComponent());
        
        // populate the initial data, if applicable
        if (super.getData()!=null) {
            String data=super.getData().value();
            if (!data.equals("")) {
                Department department=Department.getByDepartmentName(data);
                School school=department.getSchool();
                schoolsPanels.setSelected(school.getSchoolName());
            }
        }
        notifyCurrentStatus();
        
        pnlSchools.revalidate();
        getParentWindow().pack();
        
    }
    

    // school selected, populate departments of the selected school
    private void schoolSelectionChanged() {
        pnlDepartments.removeAll();
        if (schoolsPanels.isEmpty()) {
            departmentsPanels=null;
        } else {
            departmentsPanels=new LoggedDataPanelsListDepartments(schoolsPanels.getEdittedValue());

            // populate the initial data, if applicable
            if (super.getData()!=null) {
                String data=super.getData().value();
                if (!data.equals("")) {
                    Department department=Department.getByDepartmentName(data);
                    departmentsPanels.setSelected(department.getDepartmentName());
                }
            }
            departmentsPanels.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    schoolChanged();
                }
            });

            pnlDepartments.add(new JLabel("Department:"));
            pnlDepartments.add(departmentsPanels.getEditComponent());
            
        }
        notifyCurrentStatus();
        pnlDepartments.revalidate();
        getParentWindow().pack();
    }
    
    private void schoolChanged() {
        notifyCurrentStatus();
    }


    @Override
    public String getEdittedValue() {
        if (departmentsPanels==null) {
            return("");
        } else {
            return(departmentsPanels.getEdittedValue());
        }
    }

    @Override
    public boolean isEmpty() {
        return(schoolsPanels.isEmpty());
    }
    
    
    // if no school is selected, the component is valid though empty
    // if a school is selected, but no department is selected, the component is invalid
    // if both school and department are selected, the component is valid and has a value
    @Override
    public boolean isValid() {
        return(schoolsPanels.isEmpty() || (departmentsPanels!=null && (!departmentsPanels.isEmpty())));
    }
    

    private static class LoggedDataPanelsListDepartments extends LoggedDataPanelsList {

        String school;
        public LoggedDataPanelsListDepartments(String school) {
            super();
            this.school=school;
        }

        public SortedMap<String, String> getListMap() {
            SortedMap<String, String> departments=new TreeMap<String, String>();
            List<Department> allDepartments = Department.getDepartmentsOfSchool(school);
            for (Iterator<Department> it = allDepartments.iterator(); it.hasNext();) {
                Department department = it.next();
                departments.put(department.toListId(), department.toListValue());
            }
            return departments;
        }
    }

    private void notifyCurrentStatus() {
        notifyAllChangeListeners(new ChangeEvent(this));
    }

    private static class LoggedDataPanelsListSchools extends LoggedDataPanelsList {

        public LoggedDataPanelsListSchools() {
        }

        public SortedMap<String, String> getListMap() {
            SortedMap<String, String> departments=new TreeMap<String, String>();
            List<School> allDepartments = School.getAllSchools();
            for (Iterator<School> it = allDepartments.iterator(); it.hasNext();) {
                School school = it.next();
                departments.put(school.getSchoolName(), school.getSchoolName());
            }
            return departments;
        }
    }
}
